// /////////////////////////////////////////////////////////////////////////////
//
// Name:            Script.h
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :           Declaration of the Script class.
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

#ifndef __SCRIPT_H__
#define __SCRIPT_H__

// Include files
#include "../dependencies.h"
#include "../core/ReferenceCounted.h"

#ifdef __COMPILE_WITH_ANGELSCRIPT__

#include "ScriptManager.h"

// Forward declarations.
class ScriptManager;


// Script class
//! The Script class is technically a wrapper for every AngelScript file that is loaded by the
//! framework.
class Script : public ReferenceCounted
{
public:

    // Initialisation and deinitialisation
    //! Constructor
    //! @param name          Name of the script.
    Script(const std::string &name);
    //! Deconstructor
    ~Script();

    // Reference counting
    //! Will be used to instanciate objects of this class.
    //! @note For internal use only!
    static Script* refFactory(const std::string &name);
    //! Increases the reference counter.
    //! @note For internal use only!
    void refAdd();
    //! Decreases the reference counter.
    //! @note For internal use only!
    void refRelease();

    // Methods
    //! Gets the ID of this script.
    u32 getID() const;
    //! Gets the name of this script.
    const std::string& getName() const;

    //! Returns the AngelScript context associated with this Script object.
    asIScriptContext* getContext() const;
    //! Returns the AngelScript module associated with this Script object.
    asIScriptModule* getModule() const;

    //! Loads a script file and builds it.
    //! @param fileName      Filename of the file from where the script should be retrieved.
    //! @return True on successful loading, else false on failure.
    bool loadScript(const std::string &fileName);
    //! Loads a script file and builds it.
    //! @param file          Pointer to the IReadFile that will be used.
    //! @return True on successful loading, else false on failure.
    bool loadScript(IReadFile *file);
    //! Unloads the loaded script.
    //! @return True on success, false on failure.
    bool unloadScript();

    //! Executes the function with the given name and with the given arguments.
    //! @return True on success, false on failure.
    bool executeFunctionByName(const std::string &name, const std::string &args = "");

private:

    // Static members
    static u32 mIDCount;

    // Angelscript members
    s32 mRefCount;

    // Private members
    u32 mID;
    std::string mName;

    asIScriptModule *mModule;
    asIScriptContext *mContext;

};

#endif // __COMPILE_WITH_ANGELSCRIPT__

#endif
